package com.lenovo.vod.player.setting;

import java.util.Arrays;
import java.util.List;

import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.lenovo.vod.player.R;

public abstract class SettingBaseFragment extends Fragment implements View.OnKeyListener{

	protected ListView mLvOptions;
	protected ProgressDialog mProgressDialog;
	protected ImageView mIvDownIco;
	protected ImageView mIvUpIco;
	protected TextView mTvHead;

	public abstract View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState);
	
	
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		mLvOptions = (ListView) this.getActivity().findViewById
							(R.id.vodplayer_setting_lvOptions);
		mIvDownIco = (ImageView) this.getActivity().findViewById
							(R.id.vodplayer_setting_iv_Down_Ico);
		mIvUpIco = (ImageView) this.getActivity().findViewById
							(R.id.vodplayer_setting_iv_Up_Ico);
		mTvHead = (TextView) this.getActivity().findViewById
							(R.id.vodplayer_setting_tvHead);
		mLvOptions.setOnKeyListener(this);
		setListViewScrollListener();
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i("Other", "onCreate");
	}
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.i("Other", "onStart");
	}

	/**
	 * 创建联网提交信息提示对话框
	 * 
	 * @param dialogMessage
	 *            提示信息
	 * @param indeterminate
	 *            是否显示进度动画
	 */
	protected void createMessageDialog(View mView ,String dialogMessage, boolean indeterminate) {
		if (mProgressDialog != null) {
			if (mProgressDialog.isShowing()) {
				mProgressDialog.dismiss();
			}
			mProgressDialog.cancel();
			mProgressDialog = null;
		}
		mProgressDialog = new ProgressDialog(mView.getContext(),
				R.style.vodplayer_setting_newProgressDilogTheme);
		mProgressDialog.setCancelable(false);
		mProgressDialog.setMessage(dialogMessage);
		mProgressDialog.setIndeterminate(indeterminate);
		mProgressDialog.show();
	}

	/**
	 * 键盘的监听事件
	 */
	public boolean onKey(View v, int keyCode, KeyEvent event) {
			ListView lv = (ListView) v;
			int pos = lv.getSelectedItemPosition();
			int count = lv.getCount();
			if (event.getAction() == KeyEvent.ACTION_DOWN) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_B:
					hideListViewOptions();
					destroy();
					break;
				case KeyEvent.KEYCODE_ENTER:
					if (lv.getVisibility() == View.VISIBLE) {
						optionListViewItemClick(pos);
					}
					break;
				case KeyEvent.KEYCODE_DPAD_UP:
					if (pos == 0)
						pos = count - 1;
					else
						pos--;
					lv.setSelection(pos);
					break;
				case KeyEvent.KEYCODE_DPAD_DOWN:
					if (pos == count - 1)
						pos = 0;
					else
						pos++;
					lv.setSelection(pos);
					break;
				}
			} else if (event.getAction() == KeyEvent.ACTION_UP) {
				switch (keyCode) {
				case KeyEvent.KEYCODE_S:
					hideListViewOptions();
					destroy();
					break;
				}
			}
			return true;
	}
	
	/**
	 * 设置ListView的滚动监听
	 */
	protected void setListViewScrollListener() {
		mLvOptions.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if (visibleItemCount < totalItemCount) {
					mIvDownIco.setVisibility(View.VISIBLE);
				}
				if (firstVisibleItem + visibleItemCount == totalItemCount) {
					mIvDownIco.setVisibility(View.GONE);
				}
				if (firstVisibleItem != 0) {
					mIvUpIco.setVisibility(View.VISIBLE);
				} else {
					mIvUpIco.setVisibility(View.GONE);
				}
			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

		});
	}
	
	/**
	 * 修改各级菜单的标题信息
	 * 
	 * @param index
	 *            菜单项ID
	 * @param lever
	 *            级数
	 */
	protected void setHeadTextData(int index, int lever,int resourceArrayId) {
		mTvHead.setVisibility(View.VISIBLE);
		List<String> mediaOptions = null;
		mediaOptions = getStringArrayByResourceId(this.getResources(),resourceArrayId);
		mTvHead.setText(mediaOptions.get(index));
	}
	
	private static List<String> getStringArrayByResourceId( Resources res ,int arrayId){
		String[] strArr = res.getStringArray(arrayId);
		return Arrays.asList(strArr);
	}
	
	
	protected abstract void bindListViewOptionByArrays(int textArrayResId); 
	
	protected abstract void optionListViewItemClick(int index);
	
	/**
	 * 显示ListView
	 */
	protected abstract void show();
	
	/**
	 * 隐藏ListView
	 */
	protected abstract void hideListViewOptions();
	
	protected abstract void destroy();
	
	public void onDestroy() {
		super.onDestroy();
		destroy();
	}
}
	
	
	
	
